import axios from 'axios'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import defaultSettings from '@/config/defaultSettings'
import { Modal } from 'ant-design-vue'
import router from '@/router'
// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: '/khyWeb',
  timeout: 30000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
  if (error && error.response) {
    switch (error.response.status) {
        
        case 400:
            notification.error({ message: '系统提示', description: '错误请求'})
            break;
        case 401:
            localStorage.clear();
            notification.error({ message: '登陆已经过期，请重新登录', description: '错误请求'})
          //  this.$message.warning("授权过期，请重新登陆");
            router.push({ path: '/user/login'})
            break;
        case 403:
            notification.error({ message: '系统提示', description: '拒绝访问'})
            break;
        case 404:
            notification.error({ message: '系统提示', description:'很抱歉，资源未找到!'})
            break;
        case 405:
            notification.error({ message: '系统提示', description: '请求方法未允许'})
            break;
        case 408:
            notification.error({ message: '系统提示', description: '请求超时'})
            break;
        case 500:
            //默认发送错误都返回500 
            notification.error({ message: '系统提示', description: '服务器端出错'})
            break;
        case 501:
            notification.error({ message: '系统提示', description: '网络未实现'})
            break;
        case 502:
            notification.error({ message: '系统提示', description: '网络错误'})
            break;
        case 503:
            notification.error({ message: '系统提示', description: '服务不可用'})
            break;
        case 504:
            notification.error({ message: '系统提示', description: '网络超时'})
            break;
        case 505:
            notification.error({ message: '系统提示', description: '网络超时'})
            break;
        default:
            notification.error({ message: '未知错误:'+error.response.status, description: '网络超时'})
            
    }
  } else {
      notification.error({ message: '连接到服务器失败', description: '网络超时'})
    
  }
  return Promise.reject(error);
  
}

// request interceptor
request.interceptors.request.use(config => {
 // const token = storage.get(ACCESS_TOKEN)
  const token = sessionStorage.getItem(ACCESS_TOKEN);
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers[`${ACCESS_TOKEN}`] = token
  }
  if(config["data"] && config["data"]["systemNo"]){
    config.headers['systemNo']=config["data"]["systemNo"];
  }else if(config["params"] && config["params"]["systemNo"]){
    config.headers['systemNo']=config["params"]["systemNo"];
  }else{
    config.headers['systemNo']=defaultSettings.systemNo;
  }
  
  return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
  return response.data
}, errorHandler)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios,
  request as axios
}
